package com.aat.dxfy.dxyStart;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AutoCode {
	public static Connection con;

	public static void main(String[] args) throws Exception {
		List<String> tabNames=getTabNames();
		for (String tableName : tabNames) {
			String[] ss = tableName.split("_");
			String moduleName = ss[0];  //模块名称
			String str = ss[1];         //表意义
			String beanName = str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
			
			System.out.println(moduleName+" "+beanName);
			System.out.println(getTabFileds(tableName));
		}
		

		if (con != null) {
			con.close();
		}
	}

	// 得到con
	public static Connection getCon() {
		try {
			if (con == null) {
				String url = "jdbc:mysql://127.0.0.1:3306/sensesms?useUnicode=true&characterEncoding=utf8";
				String user = "root";
				String passwd = "root";
				String driver = "com.mysql.jdbc.Driver";
				Class.forName(driver);
				con = DriverManager.getConnection(url, user, passwd);
			}
		} catch (Exception e) {
		}
		return con;

	}

	// 得到表
	public static List<String> getTabNames() {
		List<String> tabs = new ArrayList<String>();
		try {
			Statement s = getCon().createStatement();
			ResultSet rs = s.executeQuery("show tables");
			while (rs.next()) {
				tabs.add(rs.getString(1));
			}
		} catch (Exception e) {
		}
		return tabs;
	}

	// 得到表字段-类型
	public static List<DbFileds> getTabFileds(String tabName) {
		List<DbFileds> fileds = new ArrayList<DbFileds>();
		try {
			String sql = "desc " + tabName;
			Statement s = getCon().createStatement();
			ResultSet rs = s.executeQuery(sql);
			while (rs.next()) {
				String key = rs.getString(1);
				String type = rs.getString(2);
				String val = "";
				if (type.contains("bigint")) {
					val = "Long";
				} else if ((!type.contains("bigint")) && type.contains("int")) {
					val = "Integer";
				} else if (type.contains("float") || type.contains("double")) {
					val = "Double";
				} else if (type.contains("char")) {
					val = "String";
				} else if (type.contains("date") || type.contains("time")) {
					val = "java.util.Date";
				} else if (type.contains("bit") || type.contains("real")) {
					val = "Boolean";
				}
				DbFileds tmp = new DbFileds();
				tmp.setName(key);
				tmp.setType(val);
				fileds.add(tmp);
			}

		} catch (Exception e) {
		}
		return fileds;
	}
	
	//

}

class DbFileds {
	private String name;
	private String type;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "DbFileds [name=" + name + ", type=" + type + "]";
	}

}